package com.promis.widget.dialog;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.promis.util.ColorUtils;
import com.promis.util.ScreenUtils;
import com.promis.widget.R;

import java.util.ArrayList;
import java.util.List;

public abstract class EditTextDialogInvoice extends Builder {

	private TextView titleView;
	private List<EditText> editTextList = new ArrayList<EditText>();

	public EditTextDialogInvoice(Activity activity) {
		this(activity, 1);
	}

	public EditTextDialogInvoice(Activity activity, int edtCount) {
		this(activity, R.style.Theme_AlertDialog, edtCount);
	}

	public EditTextDialogInvoice(Activity activity, int theme, int edtCount) {
		super(activity, theme);
		setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				EditTextDialogInvoice.this.onConfirmClick(dialog, which);
			}
		});
		setNegativeButton("取消", null);
		
		int titleHeight = ScreenUtils.dp2px(getContext(), 60);
		ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		
		LinearLayout titleLayout = new LinearLayout(activity);
		titleLayout.setLayoutParams(layoutParams);
		titleView = new TextView(activity);
		LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, titleHeight);
		titleParams.setMargins(30, 0, 30, 0);
		titleView.setLayoutParams(titleParams);
		titleView.setBackgroundResource(R.drawable.layer_stroke_blue_bottom);
		titleView.setGravity(Gravity.CENTER);
		titleLayout.addView(titleView);
		setCustomTitle(titleLayout);
		
		int editHeight = ScreenUtils.dp2px(getContext(), 50);
		LinearLayout editLayout = new LinearLayout(activity);
		editLayout.setLayoutParams(layoutParams);
		editLayout.setOrientation(LinearLayout.VERTICAL);
		
		for (int i = 0; i < edtCount; i++) {
			EditText editText = new EditText(getContext());
			LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, editHeight);
			editParams.setMargins(30, 30, 30, 30);
			editText.setLayoutParams(editParams);
			editText.setPadding(15, 0, 15, 0);
			editText.setHintTextColor(ColorUtils.getColor(activity, R.color.text_black_shallow));
			editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
			editText.setTextColor(ColorUtils.getColor(activity, R.color.text_black_deep));
			editText.setBackgroundResource(R.drawable.shape_stroke_gray_circular3);
			editLayout.addView(editText);
			editTextList.add(editText);
		}
		setView(editLayout);
	}
	
	@Override
	public Builder setTitle(CharSequence title) {
		// TODO Auto-generated method stub
		titleView.setText(title);
		return super.setTitle(title);
	}
	
	@Override
	public Builder setTitle(int titleResId) {
		// TODO Auto-generated method stub
		titleView.setText(titleResId);
		return super.setTitle(titleResId);
	}

	public EditText getEditText() {
		return editTextList.get(0);
	}
	
	public List<EditText> getEditTextList() {
		return editTextList;
	}

	public TextView getTitleView() {
		return titleView;
	}

	public abstract void onConfirmClick(DialogInterface dialog, int which);
}
